{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. Scikit-learn 简介与核心概念"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Scikit-learn (简称 sklearn) 是一个用于 Python 编程语言的免费软件机器学习库。它包含了各种分类、回归和聚类算法，包括支持向量机、随机森林、梯度提升、k-means 和 DBSCAN，并且设计用来与 Python 的数值和科学计算库 NumPy 和 SciPy 互操作。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.2 安装与环境配置\n",
    "如果你还没有安装 scikit-learn，可以通过 pip 进行安装："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install -U scikit-learn"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.3 Scikit-learn 的核心 API\n",
    "Scikit-learn 的 API 设计得非常一致和简洁。核心 API 围绕着 `Estimator` (估计器) 对象展开。\n",
    "- **Estimator (估计器)**: 任何可以从数据中学习的对象。它通过 `fit()` 方法进行学习（或“训练”），该方法接受一个数据集作为参数。\n",
    "- **Transformer (转换器)**: 一种特殊类型的估计器，可以对数据进行转换。它有 `fit()` 方法用于学习转换参数，`transform()` 方法用于应用转换。`fit_transform()` 是一个便捷方法，可以同时执行这两个步骤。\n",
    "- **Predictor (预测器)**: 另一种特殊类型的估计器，可以对新数据进行预测。它有 `fit()` 方法用于训练模型，`predict()` 方法用于生成预测。常见的预测器包括分类器和回归器。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### API 示例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.datasets import load_iris\n",
    "\n",
    "# 加载数据\n",
    "X, y = load_iris(return_X_y=True)\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
    "\n",
    "# 1. Transformer (转换器) 示例\n",
    "scaler = StandardScaler() # 实例化\n",
    "X_train_scaled = scaler.fit_transform(X_train) # 在训练数据上学习并转换\n",
    "X_test_scaled = scaler.transform(X_test) # 在测试数据上应用转换\n",
    "\n",
    "print(\"原始训练数据的前5行:\")\n",
    "print(X_train[:5])\n",
    "print(\"标准化后的训练数据前5行:\")\n",
    "print(X_train_scaled[:5])\n",
    "\n",
    "# 2. Predictor (预测器) 示例\n",
    "clf = LogisticRegression() # 实例化\n",
    "clf.fit(X_train_scaled, y_train) # 在标准化后的训练数据上训练模型\n",
    "predictions = clf.predict(X_test_scaled) # 对标准化后的测试数据进行预测\n",
    "\n",
    "print(\"测试集上的预测结果:\")\n",
    "print(predictions)\n",
    "\n",
    "# 评估预测器的性能\n",
    "accuracy = clf.score(X_test_scaled, y_test)\n",
    "print(f\"模型的准确率: {accuracy:.2f}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.4 内置数据集的加载与生成\n",
    "Scikit-learn 提供了一些内置的小型数据集，方便我们快速测试算法。\n",
    "- `load_*` 函数用于加载小型、经典的数据集。\n",
    "- `fetch_*` 函数用于下载和加载大型的真实世界数据集。\n",
    "- `make_*` 函数用于生成具有特定属性的合成数据集。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.datasets import load_iris, make_classification\n",
    "\n",
    "# 加载鸢尾花数据集\n",
    "iris = load_iris()\n",
    "\n",
    "# 数据集是一个 Bunch 对象，类似于字典\n",
    "print(f\"数据集的键: {iris.keys()}\")\n",
    "\n",
    "# 特征数据 (X)\n",
    "print(f\"特征数据的形状: {iris.data.shape}\")\n",
    "\n",
    "# 标签数据 (y)\n",
    "print(f\"标签数据的形状: {iris.target.shape}\")\n",
    "\n",
    "# 特征名称\n",
    "print(f\"特征名称: {iris.feature_names}\")\n",
    "\n",
    "# 标签名称\n",
    "print(f\"标签名称: {iris.target_names}\")\n",
    "\n",
    "# 数据集描述\n",
    "# print(iris.DESCR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 生成一个用于分类的合成数据集\n",
    "X_synthetic, y_synthetic = make_classification(\n",
    "    n_samples=100, # 样本数\n",
    "    n_features=10, # 特征数\n",
    "    n_informative=5, # 有用特征数\n",
    "    n_redundant=0, # 冗余特征数\n",
    "    n_classes=2, # 类别数\n",
    "    random_state=42\n",
    ")\n",
    "\n",
    "print(\"合成数据集的特征形状:\", X_synthetic.shape)\n",
    "print(\"合成数据集的标签形状:\", y_synthetic.shape)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ml311",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
